package com.duo.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.duo.dto.TableDto;
import com.duo.entity.Table;
import com.duo.exception.BaseException;
import com.duo.result.BaseResult;
import com.duo.service.TableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: Xrx
 * @Description:
 * @CreateTime: 2023/7/10 20:54
 */
@Slf4j
@RestController
@RequestMapping("/admin/table")
public class TableController {


    @Autowired
    private TableService tableService;


    /**
     * 添加桌子
     */
    @PostMapping("/save")
    public Object addTable(@RequestBody TableDto tableDto) {
        if (StrUtil.isBlank(tableDto.getName())) {
            throw new BaseException("桌子名称不能为空");
        }
        Table table = new Table();
        BeanUtils.copyProperties(tableDto, table);
        tableService.save(table);
        return BaseResult.success("添加成功");
    }

    /**
     * 删除桌子
     */
    @DeleteMapping("/delete/{id}")
    public Object deleteTable(@PathVariable("id") Long id) {
        if (ObjectUtil.isNull(id)) {
            throw new BaseException("id不能为空");
        }
        tableService.removeById(id);
        return BaseResult.success("删除成功");
    }

    /**
     * 查询所有桌子
     */
    @GetMapping("/list")
    public Object selectTable() {
        List<Table> list = tableService.list();
        log.info("查询到的桌子： {}", list);
        return BaseResult.success("查询成功", list);
    }

    /**
     * 根据id查询桌子
     */
    @GetMapping("/selectById/{id}")
    public Object selectTableById(@PathVariable("id") Long id) {
        if (id == null) {
            throw new BaseException("id不能为空");
        }
        return BaseResult.success("查询成功", tableService.getById(id));
    }




}
